package com.lh.pospi.pospi.login;

import android.arch.lifecycle.ViewModelProviders;
import android.databinding.Observable;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;

import com.lh.pospi.pospi.BR;
import com.lh.pospi.pospi.R;
import com.lh.pospi.pospi.databinding.ActivityLoginBinding;

import me.goldze.mvvmhabit.base.BaseActivity;

public class LoginActivity extends BaseActivity<ActivityLoginBinding,LoginViewModel> {
    @Override
    public int initContentView(Bundle bundle) {
        return R.layout.activity_login;
    }

    @Override
    public int initVariableId() {
        return BR.vm;
    }

    @Override
    public LoginViewModel initViewModel() {
        return ViewModelProviders.of(this).get(LoginViewModel.class);
    }

    @Override
    public void initViewObservable() {
        viewModel.pwdShow.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                if (viewModel.pwdShow.get()) {
                    binding.ivShowPwd.setImageResource(R.mipmap.show_psw_press);
                    binding.etPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    binding.etPwd.setSelection(viewModel.pwd.get().length());
                } else {
                    binding.ivShowPwd.setImageResource(R.mipmap.show_psw);
                    binding.etPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    binding.etPwd.setSelection(viewModel.pwd.get().length());
                }
            }
        });
    }
}
